package com.example.coffeeseller_2.model.helper.view_helper;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.coffeeseller_2.R;
import com.example.coffeeseller_2.model.beans.BitmapCacheBean;
import com.example.coffeeseller_2.model.beans.Coffee;
import com.example.coffeeseller_2.model.contentprovider.ApplicationConstance;
import com.example.coffeeseller_2.utils.BitmapUtil;
import com.example.coffeeseller_2.utils.DensityUtil;
import com.example.coffeeseller_2.utils.GlideUtil;
import com.example.coffeeseller_2.widget.CoffeeRoundImage;
import com.example.coffeeseller_2.widget.CoffeeTwoRoundImage;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WH on 2018/7/18.
 * 创建咖啡布局
 */

public class CreateCoffeeLayoutHelper {

    private static String TAG = "CreateCoffeeLayoutHelper";
    public static int OnePageCoffeeNum = 5;
    private int pageCount;
    private GlideUtil glideUtil;
    Handler handler;

    public CreateCoffeeLayoutHelper() {
        glideUtil = new GlideUtil();
        handler = new Handler();
    }

    public void init(List<Coffee> coffees) {

        pageCount = coffees.size() / OnePageCoffeeNum;
        pageCount = (coffees.size() % OnePageCoffeeNum > 0 ? pageCount + 1 : pageCount);
    }

    public int getPageCount() {
        return pageCount;
    }

    public int getStartIndex(int curPage) {

        int page = curPage % pageCount;
        int startIndex = page * OnePageCoffeeNum;
        return startIndex;
    }

    public List<Coffee> getOnePageCoffees(int curPage, List<Coffee> coffees) {

        int page = curPage % pageCount;
        int startIndex = page * OnePageCoffeeNum;
        int endIndex = ((page + 1) * OnePageCoffeeNum) > coffees.size() ? coffees.size() : ((page + 1) * OnePageCoffeeNum);
        List<Coffee> coffeeList = new ArrayList<>();

        for (int i = startIndex; i < endIndex; i++) {
            coffeeList.add(coffees.get(i));
        }
        return coffeeList;
    }

    public View getView(Context context) {
       int width = (int) context.getResources().getDimension(R.dimen.coffee_item_width);
       int height = (int) context.getResources().getDimension(R.dimen.coffee_item_height);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(RecyclerView.LayoutParams.WRAP_CONTENT,
                RecyclerView.LayoutParams.WRAP_CONTENT);//RecyclerView.LayoutParams.WRAP_CONTENT:用这种无效，最终结果还是MATCH_PARENT
        params.gravity = Gravity.CENTER;
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_buy_recycle_item, null, false);
        view.setLayoutParams(params);
        view.setVisibility(View.GONE);
        return view;
    }

    public View getView(final Context context, List<Coffee> coffees, final int i) {
       int width = (int) context.getResources().getDimension(R.dimen.coffee_item_width);
        int height = (int) context.getResources().getDimension(R.dimen.coffee_item_height);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(RecyclerView.LayoutParams.WRAP_CONTENT,
                RecyclerView.LayoutParams.WRAP_CONTENT);//RecyclerView.LayoutParams.WRAP_CONTENT:用这种无效，最终结果还是MATCH_PARENT
        params.gravity = Gravity.CENTER;
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_buy_recycle_item, null, false);
    /*    if (i!=0){
            params.setMargins(DensityUtil.dp2px(context, 36),0, 0, 0);

        }*/
        view.setLayoutParams(params);
        bindDatas(context, view, coffees.get(i));

        return view;
    }


    public LinearLayout getLinearLayout(final Context context, LinearLayout parentLayout, List<Coffee> coffees) {

        for (int i = 0; i < coffees.size(); i++) {

            final int index = i;
            RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(500,
                    RecyclerView.LayoutParams.MATCH_PARENT);//RecyclerView.LayoutParams.WRAP_CONTENT:用这种无效，最终结果还是MATCH_PARENT
            // params.setMargins(DensityUtil.dp2px(context, context.getResources().getDimension(R.dimen.fragment_buy_coffee_space)), 0, 0, 0);
            View view = LayoutInflater.from(context).inflate(R.layout.fragment_buy_recycle_item, parentLayout, false);

            view.setLayoutParams(params);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "onClick= " + index, Toast.LENGTH_SHORT).show();
                }
            });
            bindDatas(context, view, coffees.get(i));
            parentLayout.addView(view);
        }
        return parentLayout;
    }

    public void bindDatas(final Context context, final View view, final Coffee coffee) {
        final CoffeeRoundImage mCoffeeImg = (CoffeeRoundImage) view.findViewById(R.id.coffeeImg);
        final CoffeeTwoRoundImage mLayerImg = (CoffeeTwoRoundImage) view.findViewById(R.id.layerImg);
        TextView mCoffeeWaterType = (TextView) view.findViewById(R.id.coffeeWaterType);
        TextView mCoffeeSpecialImg = (TextView) view.findViewById(R.id.coffeeSpecialImg);
        TextView mCoffeeName = (TextView) view.findViewById(R.id.coffeeName);
        TextView mCoffeePrice = (TextView) view.findViewById(R.id.coffeePrice);
        TextView mCoffeeActivePrice = (TextView) view.findViewById(R.id.coffeeActivePrice);
        RelativeLayout mOverLayout = (RelativeLayout) view.findViewById(R.id.overLayout);
        LinearLayout mPriceLayout = (LinearLayout) view.findViewById(R.id.priceLayout);
        ImageView mOverImg = (ImageView) view.findViewById(R.id.overImg);

        mCoffeeName.setText(coffee.getName());


        if (coffee.getWaterType() == 0) {
            mCoffeeWaterType.setBackgroundResource(R.mipmap.cold);
        } else if (coffee.getWaterType() == 1) {
            mCoffeeWaterType.setBackgroundResource(R.mipmap.hot);

        }

        if (TextUtils.isEmpty(coffee.getActivitiesPrice())
                || TextUtils.equals(coffee.getActivitiesPrice(), "null")
                || Float.parseFloat(coffee.getActivitiesPrice()) <= 0) {//没有活动价
            mCoffeePrice.setText(coffee.getPrice());
            mPriceLayout.setVisibility(View.GONE);
            mCoffeeSpecialImg.setVisibility(View.GONE);

        } else {//有活动价
            mCoffeePrice.setText(coffee.getActivitiesPrice());
            mCoffeeActivePrice.setText(coffee.getPrice());
            mPriceLayout.setVisibility(View.VISIBLE);
            mCoffeeSpecialImg.setVisibility(View.VISIBLE);
        }

        if (coffee.isOver) {
            mOverLayout.setVisibility(View.VISIBLE);
        } else {
            mOverLayout.setVisibility(View.GONE);

        }

        if (coffee.getImageBitmap() != null && coffee.getCropBitmap() != null) {
            mCoffeeImg.setImageBitmap(coffee.getImageBitmap());
            mLayerImg.setImageBitmap(coffee.getCropBitmap());
        } else {
            if (ApplicationConstance.getInstance().getBitmapCacheBeanMap().containsKey(coffee.cacheUrl)) {
                BitmapCacheBean bitmapCacheBean = ApplicationConstance.getInstance().getBitmapCacheBeanMap().get(coffee.cacheUrl);
                if (bitmapCacheBean != null
                        && TextUtils.equals(bitmapCacheBean.getUrl(), coffee.cacheUrl)
                        && bitmapCacheBean.getImage() != null && bitmapCacheBean.getGaosiImage() != null) {
                    coffee.setImageBitmap(bitmapCacheBean.getImage());
                    coffee.setCropBitmap(bitmapCacheBean.getGaosiImage());
                    mLayerImg.setImageBitmap(bitmapCacheBean.getGaosiImage());
                    mCoffeeImg.setImageBitmap(bitmapCacheBean.getImage());
                    return;
                }
            }

            if (glideUtil == null) {
                glideUtil = new GlideUtil();
            }
            if (handler == null) {
                handler = new Handler();
            }
            if (context == null) {
                return;
            }
            glideUtil.getBitmap(context, coffee.getCacheUrl(), new GlideUtil.GlideBitmapListener() {
                @Override
                public void getBitmap(final Bitmap bitmap) {

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Bitmap resource = bitmap;
                            float height = context.getResources().getDimension(R.dimen.coffee_gaosi_img_height);
                            if (resource == null) {
                                resource = BitmapFactory.decodeResource(context.getResources(), R.mipmap.no_coffee);
                            }
                            float gaosiHeight = DensityUtil.dp2px(context, height) / (float) mCoffeeImg.getHeight();
                            final Bitmap lb = BitmapUtil.ImageCropWithRect(resource, gaosiHeight);
                            final Bitmap newBitmap = BitmapUtil.fastblur(lb, 1f, 25);
                            // final Bitmap bitmap1 = BitmapUtil.ImageCropWithRect(resource, 0, (int) (mCoffeeImg.getHeight() - gaosiHeight));
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    coffee.setImageBitmap(bitmap);
                                    coffee.setCropBitmap(newBitmap);
                                    mLayerImg.setImageBitmap(newBitmap);
                                    mCoffeeImg.setImageBitmap(bitmap);
//                                    view.setTag(bitmap);//bitmap1

                                    BitmapCacheBean bitmapCacheBean = new BitmapCacheBean();
                                    bitmapCacheBean.setUrl(coffee.getCacheUrl());
                                    bitmapCacheBean.setImage(bitmap);
                                    bitmapCacheBean.setGaosiImage(newBitmap);
                                    ApplicationConstance.getInstance().getBitmapCacheBeanMap().put(coffee.getCacheUrl(), bitmapCacheBean);
                                }
                            });
                        }
                    }).start();
                }
            });
        }
    }

}
